﻿using PSDClientService.SystemSettings;
using PSDLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace PSDClientService
{
    partial class PSDWinService : ServiceBase
    {
        ClientServiceContext context;

        public PSDWinService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            if (AppSettings.Enabled)
            {
                RunAsService(AppSettings.Setting.PWD, AppSettings.Setting.LogType);
            }
        }

        private void RunAsService(string password, string logtype)
        {
            context = new ClientServiceContext();
            var result = context.Init(password, logtype, true);

            if (result.success)
            {
                context.Start();

                context.Logger.Info("PSDClientService is Running...");
            }
            else
            {
                context.Logger.Error("PSDClientService Failed：" + result.message);
            }

        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。

            if (context != null)
            {
                context.Stop();
                context.Logger.Info("PSDClientService Stopped");
                context.Dispose();
            }
        }
    }
}
